package com.work.yly.website.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.work.yly.website.service.Constant;
import com.work.yly.website.service.ServiceDefine;
import com.work.yly.website.service.ServiceHelper;
import com.work.yly.website.vo.Hyxx;
import com.work.yly.website.vo.Zjtp;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/my")
public class MyWealthController extends BaseController {
	@RequestMapping("/index")
	public String index(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Object hy_id = session.getAttribute("hy_id");
		if (null == hy_id) {
			return "redirect:../login?successUrl=/my/index";
		}
		JSONObject obj = ServiceHelper.getMyWealth(Integer.valueOf(hy_id.toString()));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			JSONObject data = obj.getJSONObject("data");
			model.addAttribute("kyje", data.getDouble("kyje"));
			model.addAttribute("lcje", data.getDouble("lcje"));
			model.addAttribute("yjsy", data.getDouble("yjsy"));
			model.addAttribute("fcbje", data.getInt("fcbje"));
			session.setAttribute("hasLoan", data.getInt("hasLoan"));
		} else {
			model.addAttribute("error", obj.getString("message"));
		}
		
		obj = ServiceHelper.queryCurJkqk(Integer.valueOf(hy_id.toString()));

		if(obj.getInt("flag") == ServiceDefine.CALL_SUCCESS){
			JSONObject data = obj.getJSONObject("data");
//			Type type = new TypeToken<ArrayList<Map<String, Object>>>() {}.getType(); 
//			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
//			List<Map<String, Object>> list = gson.fromJson(data.getString("jkqk_list"), type);
//			System.out.println(list.size());
			JSONArray arr = data.getJSONArray("jkqk_list");			
			for(int i=0; i<arr.size(); i++){
				JSONObject o = arr.getJSONObject(i);
				Map<String, Object> m = Maps.newHashMap();
				String jklx = o.getString("jklx");
				m.put("jkdh", o.getString("jkdh"));
				m.put("yhkh", o.getString("yhkh"));
				m.put("sjfkje", o.getDouble("sjfkje"));
				m.put("jkje", o.getDouble("jkje"));
				m.put("jksj", o.getString("jksj"));
				if(o.getInt("cwfkbs")==0){
					m.put("cwfkbs", "未放款");
				}
				else{
					m.put("cwfkbs", "已放款");
				}
				m.put("qrr_qrsj", o.getString("qrr_qrsj"));
				m.put("hk_shr_shsj", o.getString("hk_shr_shsj"));
				if(o.getInt("hksq")==0){
					m.put("hksq", "未申请");
				}
				else{
					m.put("hksq", "已申请");
				}
				if(o.getInt("cwfkbs")==1 && o.getInt("hksq")==0){
					m.put("can", 1);
				}
				else{
					m.put("can", 0);
				}
				m.put("id", o.getInt("id"));
				model.addAttribute("jklx_"+jklx, m);
			}
		}
		
		return "/my/index";
	}

	@RequestMapping("/hyxx")
	public String hyxx(HttpServletRequest request, Model model, boolean err) {
		if (!err) {
			HttpSession session = request.getSession();
			Object hy_id = session.getAttribute("hy_id");
			if (null != hy_id) {
				JSONObject obj = ServiceHelper.getHyxx(Integer.valueOf(hy_id.toString()));
				if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
					JSONObject data = obj.getJSONObject("data").getJSONObject("hyxx");
					Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
					Hyxx hyxx = gson.fromJson(data.toString(), Hyxx.class);
					model.addAttribute("hyxx", hyxx);
					model.addAttribute("banklist", Constant.bankDef());
					model.addAttribute("hyzklist", Constant.hyzkDef());
					model.addAttribute("xlqklist", Constant.xlqkDef());
					model.addAttribute("fczklist", Constant.fczkDef());
					model.addAttribute("dwxzlist", Constant.dwxzDef());
					model.addAttribute("jtlxrlist", Constant.jtLxrGxDef());
					model.addAttribute("jjlxrlist", Constant.jjLxrGxDef());
					
				} else {
					model.addAttribute("error", obj.getString("message"));
				}
			} else {
				return "redirect:../login?successUrl=/my/hyxx";				
			}
		}
		String flag = request.getParameter("flag");
		String jklx = request.getParameter("jklx");
		String dojk = request.getParameter("dojk");
		model.addAttribute("jklx", jklx);
		
		if (StringUtils.isNotEmpty(flag)) {
			model.addAttribute("flag", flag);
			
			if(StringUtils.isEmpty(dojk)){
				dojk = "0";
			}
			if(dojk.equals("2")){
				model.addAttribute("lccp_id", request.getParameter("lccp_id"));
			}
			model.addAttribute("dojk", dojk);
			if (flag.toString().equals("1")) {				
				return "/my/hyxx_grjk";
			}
			if (flag.toString().equals("2")) {
				return "/my/hyxx_cljk";
			}
			if (flag.toString().equals("3")) {
				return "/my/hyxx_fcjk";
			}
			if (flag.toString().equals("4")) {
				return "/my/hyxx_qyjk";
			}
		}
		return "/my/hyxx";
	}

	@RequestMapping("/savehyxx")
	public String savehyxx(Hyxx hyxx, HttpServletRequest request, Model model) {
		String flag = request.getParameter("flag");
		String dojk = request.getParameter("dojk");
		String jklx = request.getParameter("jklx");
		if (null != flag) {
			JSONObject ret = null;
			if (flag.equals("0")) {
				ret = ServiceHelper.modHybaseInfo(hyxx);
			} else {
				ret = ServiceHelper.modHyJkInfo(hyxx, flag.toString());
			}

			if (ret.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
				
				model.addAttribute("error", ret.getString("message"));
			} else {
				model.addAttribute("hyxx", hyxx);
				model.addAttribute("error", ret.getString("message"));
				model.addAttribute("banklist", Constant.bankDef());
				model.addAttribute("hyzklist", Constant.hyzkDef());
				model.addAttribute("xlqklist", Constant.xlqkDef());
				model.addAttribute("fczklist", Constant.fczkDef());
				model.addAttribute("dwxzlist", Constant.dwxzDef());
				model.addAttribute("jtlxrlist", Constant.jtLxrGxDef());
				model.addAttribute("jjlxrlist", Constant.jjLxrGxDef());
				model.addAttribute("flag", flag);
				model.addAttribute("dojk", dojk);
				model.addAttribute("jklx", jklx);
				
				ret = ServiceHelper.getJkcl(hyxx.getId(), jklx);
				JSONArray o = ret.getJSONObject("data").getJSONArray("jkcl_list");
				List<Zjtp> list = Lists.newArrayList();
				for(int i=0; i< o.size(); i++){
					JSONObject o1 = o.getJSONObject(i);
					Gson gson = new GsonBuilder().create();
					Zjtp zjtp = gson.fromJson(o1.toString(), Zjtp.class);
					list.add(zjtp);
				}
				if(jklx.equals("1")){
					hyxx.setGrjkcl(list);
				}
				if(jklx.equals("2")){
					hyxx.setCljkcl(list);
				}
				if(jklx.equals("3")){
					hyxx.setFcjkcl(list);
				}
				if(jklx.equals("4")){
					hyxx.setQyjkcl(list);
				}
				
				return hyxx(request, model, true);
			}
		}
		if(StringUtils.isNotEmpty(dojk)&&(dojk.equals("1"))){
			if(flag.equals("0")){
				return "redirect:../loan/wantload?jklx="+jklx;
			}
			//TODO 要验证登录是否有效
			String jkje = request.getParameter("jkje");
			if(StringUtils.isEmpty(jkje)){
				jkje = "0";
			}
			String jkqx = request.getParameter("jkqx");
			if(StringUtils.isEmpty(jkqx)){
				jkqx = "0";
			}			
			String jkyt = request.getParameter("jkyt");
			int jdlv_id = 0;
			if(jklx.equals("1")) jdlv_id =1;
			
			JSONObject ret = ServiceHelper.saveJkxx(hyxx.getId(), jklx, Double.valueOf(jkje), Integer.valueOf(jkqx), jkyt, jdlv_id);
			if (ret.getInt("flag") == ServiceDefine.CALL_SUCCESS) {				
				model.addAttribute("error", ret.getString("message"));
			} else {
				model.addAttribute("hyxx", hyxx);
				model.addAttribute("jkje", jkje);
				model.addAttribute("jkqx", jkqx);
				model.addAttribute("jkyt", jkyt);				
				model.addAttribute("error", ret.getString("message"));
				model.addAttribute("banklist", Constant.bankDef());
				model.addAttribute("hyzklist", Constant.hyzkDef());
				model.addAttribute("xlqklist", Constant.xlqkDef());
				model.addAttribute("fczklist", Constant.fczkDef());
				model.addAttribute("dwxzlist", Constant.dwxzDef());
				model.addAttribute("jtlxrlist", Constant.jtLxrGxDef());
				model.addAttribute("jjlxrlist", Constant.jjLxrGxDef());
				model.addAttribute("flag", flag);
				model.addAttribute("dojk", dojk);
				model.addAttribute("jklx", jklx);
				ret = ServiceHelper.getJkcl(hyxx.getId(), jklx);
				JSONArray o = ret.getJSONObject("data").getJSONArray("jkcl_list");
				List<Zjtp> list = Lists.newArrayList();
				for(int i=0; i< o.size(); i++){
					JSONObject o1 = o.getJSONObject(i);
					Gson gson = new GsonBuilder().create();
					Zjtp zjtp = gson.fromJson(o1.toString(), Zjtp.class);
					list.add(zjtp);
				}
				if(jklx.equals("1")){
					hyxx.setGrjkcl(list);
				}
				if(jklx.equals("2")){
					hyxx.setCljkcl(list);
				}
				if(jklx.equals("3")){
					hyxx.setFcjkcl(list);
				}
				if(jklx.equals("4")){
					hyxx.setQyjkcl(list);
				}
				return hyxx(request, model, true);
			}
			
			return "redirect:/my/hisloan"; 
		}
		else if(dojk.equals("2")){
			String lccp_id= request.getParameter("lccp_id");
			return "redirect:../finance/lc_detail?lccp_id="+lccp_id;
		}
		return "redirect:../loan/index";
	}

	@RequestMapping("/hisloan")
	public String hisload(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Object hy_id = session.getAttribute("hy_id");
		if (null == hy_id) {
			return "redirect:../login?successUrl=/my/hisloan";
		}
		JSONObject obj = ServiceHelper.queryHisJkqk(Integer.valueOf(hy_id.toString()));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {		
			JSONArray data = obj.getJSONObject("data").getJSONArray("jkqk_list");
			List<Map<String, Object>> grjk_list = Lists.newArrayList();
			List<Map<String, Object>> cljk_list = Lists.newArrayList();
			List<Map<String, Object>> fcjk_list = Lists.newArrayList();
			List<Map<String, Object>> qyjk_list = Lists.newArrayList();
			for(int i=0; i< data.size(); i++){
				JSONObject o = data.getJSONObject(i);
				Map<String, Object> map = Maps.newHashMap();
				String jklx = o.getString("jklx");
				map.put("jklx", jklx);
				map.put("jksj", o.getString("jksj"));
				map.put("hksj", o.getString("hksj"));
				map.put("sjhksj", o.getString("sjhksj"));
				map.put("jklv", o.getDouble("jklv"));
				map.put("jkje", o.getDouble("jkje"));
				map.put("sjfkje", o.getDouble("sjfkje"));
				map.put("sjhkje", o.getDouble("sjhkje"));
				if(jklx.equals("1")){
					grjk_list.add(map);
				}
				if(jklx.equals("2")){
					cljk_list.add(map);
				}
				if(jklx.equals("3")){
					fcjk_list.add(map);
				}
				if(jklx.equals("4")){
					qyjk_list.add(map);
				}			
			}
			model.addAttribute("grjk_list", grjk_list);
			model.addAttribute("cljk_list", cljk_list);
			model.addAttribute("fcjk_list", fcjk_list);
			model.addAttribute("qyjk_list", qyjk_list);
		}
		else{
			model.addAttribute("error", obj.getString("message"));
		}
		return "/my/myhisloan";
	}
	
	@RequestMapping("/hislc")
	public String hislc(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Object hy_id = session.getAttribute("hy_id");
		if (null == hy_id) {
			return "redirect:../login?successUrl=/my/hislc";
		}
		JSONObject obj = ServiceHelper.queryLcxxByHyId(Integer.valueOf(hy_id.toString()));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {		
			JSONArray data = obj.getJSONObject("data").getJSONArray("lcxx_list");
			List<Map<String, Object>> list = Lists.newArrayList();
			for(int i=0; i< data.size(); i++){
				JSONObject o = data.getJSONObject(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("cpmc", o.getString("cpmc"));
				map.put("gmsj", o.getString("gmsj"));
				map.put("cpjs", o.getString("cpjs"));
				map.put("fbq", o.getInt("fbq"));
				map.put("syl", o.getDouble("syl"));
				map.put("sxsj", o.getString("sxsj"));
				map.put("gmje", o.getDouble("gmje"));
				map.put("yqsy", o.getDouble("yqsy"));
				map.put("fcbje", o.getInt("fcbje"));				
				map.put("orderId", o.getString("orderId"));			
				map.put("zfzt", o.getString("zfzt"));
				if("Y".equals(o.getString("sfdq"))){
					map.put("sfdq", "已到期");
				}
				else{
					map.put("sfdq", "未到期");
				}
				list.add(map);
			}
			model.addAttribute("lcxx_list", list);
		}
		else{
			model.addAttribute("error", obj.getString("message"));
		}
		
		return "/my/myhislc";
	}
	@RequestMapping("/fcb")
	public String fcb(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Object hy_id = session.getAttribute("hy_id");
		if (null == hy_id) {
			return "redirect:../login?successUrl=/my/fcb";
		}
		JSONObject obj = ServiceHelper.getMyWealth(Integer.valueOf(hy_id.toString()));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			model.addAttribute("fcbje", obj.getJSONObject("data").getInt("fcbje"));
		}
		else{
			model.addAttribute("error", obj.getString("message"));
		}
		return "/my/fcb";
	}
	@RequestMapping("/fcblist")
	public String fcblist(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Object zh_id = session.getAttribute("zh_id");
		if (null == zh_id) {
			return "redirect:../login?successUrl=/my/fcblist";
		}
		String pagenum = request.getParameter("pagenum");
		String pagesize = request.getParameter("pagesize");
		if(StringUtils.isEmpty(pagenum)){
			pagenum = "1";
		}
		if(StringUtils.isEmpty(pagesize)){
			pagesize = "5";
		}
		
		JSONObject obj = ServiceHelper.queryFcbbgmx(Integer.valueOf(zh_id.toString()), Integer.valueOf(pagenum), Integer.valueOf(pagesize));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			JSONArray arr = obj.getJSONObject("data").getJSONArray("fcbbg_list");
			List<Map<String, Object>> list = Lists.newArrayList();
			for(int i=0; i<arr.size(); i++){
				JSONObject o = arr.getJSONObject(i);
				Map<String, Object> map = Maps.newHashMap();
				switch(o.getInt("bglx")){
					case 0: map.put("bglx", "新用户注册奖励");
					case 1: map.put("bglx", "摇钱树摇奖获得");
					case 2: map.put("bglx", "推荐新会员获得");
					case 9: map.put("bglx", "购买理财消费");
					default:;
				}
				map.put("bgsj", o.getString("bgsj"));
				map.put("fcbje", o.getInt("fcbje"));
				map.put("note", o.getString("note"));
				list.add(map);				
			}
			model.addAttribute("totalCount", obj.getJSONObject("data").getInt("totalCount"));
			model.addAttribute("pagenum", obj.getJSONObject("data").getInt("pagenum"));
			model.addAttribute("pagesize", obj.getJSONObject("data").getInt("pagesize"));
			model.addAttribute("list", list);
		}
		else{
			model.addAttribute("error", obj.getString("message"));
		}		
		return "/my/fcblist";
	}
	
	@RequestMapping("/modpass")
	public String forgetpass(HttpServletRequest request, Model model) {	
		String sjhm = request.getParameter("sjhm");
		String dxyzm = request.getParameter("dxyzm");
		String password = request.getParameter("password");
		if(StringUtils.isNotEmpty(sjhm)&&StringUtils.isNotEmpty(dxyzm)&&StringUtils.isNotEmpty(password)){
			JSONObject obj = ServiceHelper.restpwd(sjhm, dxyzm, password);
			model.addAttribute("error", obj.getString("message"));
		}
		return "/my/forgetpass";
	}
	
	@RequestMapping("/message")
	public String sysnotify(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Object zh_id = session.getAttribute("zh_id");
		if (null == zh_id) {
			return "redirect:../login?successUrl=/my/fcblist";
		}
		String pagenum = request.getParameter("pagenum");
		String pagesize = request.getParameter("pagesize");
		if(StringUtils.isEmpty(pagenum)){
			pagenum = "1";
		}
		if(StringUtils.isEmpty(pagesize)){
			pagesize = "5";
		}
		
		JSONObject obj = ServiceHelper.queryTsxx(Integer.valueOf(zh_id.toString()), Integer.valueOf(pagenum), Integer.valueOf(pagesize));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			JSONArray arr = obj.getJSONObject("data").getJSONArray("tsxx_list");
			List<Map<String, Object>> list = Lists.newArrayList();
			for(int i=0; i<arr.size(); i++){
				JSONObject o = arr.getJSONObject(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("tssj", o.getString("tssj"));
				map.put("xxnr", o.getString("xxnr"));
				switch (o.getInt("cznr")){
					case 0: map.put("cznr", "借款审核通过");
					case 1: map.put("cznr", "放款通知");
					case 2: map.put("cznr", "还款审核通过");
					case 3: map.put("cznr", "还款提醒");
					case 4: map.put("cznr", "理财到期提醒");
					case 5: map.put("cznr", "活动通知");
					case 6: map.put("cznr", "其他提醒通知");
					case 7: map.put("cznr", "借款审核不通过");
					case 8: map.put("cznr", "还款审核不通过");
					default:;
				}				
				if(o.getInt("czbs")==0){
					map.put("czbs", "理财消息");
				}
				else{
					map.put("czbs", "借款消息");
				}
				list.add(map);				
			}
			model.addAttribute("totalCount", obj.getJSONObject("data").getInt("totalCount"));
			model.addAttribute("pagenum", obj.getJSONObject("data").getInt("pagenum"));
			model.addAttribute("pagesize", obj.getJSONObject("data").getInt("pagesize"));
			model.addAttribute("list", list);
		}
		else{
			model.addAttribute("error", obj.getString("message"));
		}		
		return "/my/msg";
	}
	
	@RequestMapping("/upjkcl")
	public String uploadjk(HttpServletRequest request, Model model) {
		model.addAttribute("hy_id", request.getParameter("hy_id"));
		model.addAttribute("zpzl", request.getParameter("zpzl"));
		model.addAttribute("id", request.getParameter("id"));
		model.addAttribute("idx", request.getParameter("idx"));
		return "/my/upload";
	}
	@RequestMapping("/uphkpz")
	public String uploadhk(HttpServletRequest request, Model model) {
		model.addAttribute("hy_id", request.getParameter("hy_id"));		
		model.addAttribute("id", request.getParameter("id"));		
		return "/my/upload2";
	}	
}
